/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * telaVendas.java
 *
 *
 */

package Telas;
import BD.*;
import gerenciadorveiculos.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Gabriel
 */
public class telaVendas extends javax.swing.JFrame {

    /** Creates new form telaVendas */
    public telaVendas() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonEfetuar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButtonVeiculo = new javax.swing.JButton();
        jLabelVeiculo = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableVeiculo = new javax.swing.JTable();
        jLabelModelo = new javax.swing.JLabel();
        jTextModelo = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jTextCPF = new javax.swing.JTextField();
        jLabelCPF = new javax.swing.JLabel();
        jLabelCliente = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableCliente = new javax.swing.JTable();
        jButtonCliente = new javax.swing.JButton();
        jTextValor = new javax.swing.JTextField();
        jLabelValor = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabelObs = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBounds(new java.awt.Rectangle(500, 200, 500, 500));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButtonEfetuar.setText("Efetuar Venda");
        jButtonEfetuar.setName("jButtonEfetuar"); // NOI18N
        jButtonEfetuar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEfetuarActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonEfetuar, new org.netbeans.lib.awtextra.AbsoluteConstraints(710, 460, -1, -1));

        jPanel1.setName("jPanel1"); // NOI18N

        jButtonVeiculo.setText("Pesquisar Veiculo");
        jButtonVeiculo.setName("jButtonVeiculo"); // NOI18N
        jButtonVeiculo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVeiculoActionPerformed(evt);
            }
        });

        jLabelVeiculo.setText("Veiculo");
        jLabelVeiculo.setName("jLabelVeiculo"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTableVeiculo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Chassi", "Modelo", "Ano", "PrecoVenda"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTableVeiculo.setName("jTableVeiculo"); // NOI18N
        jScrollPane2.setViewportView(jTableVeiculo);

        jLabelModelo.setText("Modelo");
        jLabelModelo.setName("jLabelModelo"); // NOI18N

        jTextModelo.setName("jTextModelo"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabelVeiculo, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(722, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 794, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelModelo, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jTextModelo, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(94, 94, 94)
                        .addComponent(jButtonVeiculo)))
                .addGap(56, 56, 56))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabelVeiculo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonVeiculo)
                    .addComponent(jLabelModelo)
                    .addComponent(jTextModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 230, -1, -1));

        jPanel2.setName("jPanel2"); // NOI18N

        jTextCPF.setName("jTextCPF"); // NOI18N

        jLabelCPF.setText("CPF:");
        jLabelCPF.setName("jLabelCPF"); // NOI18N

        jLabelCliente.setText("Cliente");
        jLabelCliente.setName("jLabelCliente"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTableCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Nome", "CPF", "Telefone", "Endereco"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTableCliente.setName("jTableCliente"); // NOI18N
        jScrollPane1.setViewportView(jTableCliente);

        jButtonCliente.setText("Pesquisar Cliente");
        jButtonCliente.setName("jButtonCliente"); // NOI18N
        jButtonCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(337, 337, 337)
                .addComponent(jLabelCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53)
                .addComponent(jTextCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(93, 93, 93)
                .addComponent(jButtonCliente)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 796, Short.MAX_VALUE)
                .addGap(54, 54, 54))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(751, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCliente)
                    .addComponent(jTextCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelCPF)))
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, -1));

        jTextValor.setName("jTextValor"); // NOI18N
        getContentPane().add(jTextValor, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 400, 100, 20));

        jLabelValor.setText("Valor Vendido");
        jLabelValor.setName("jLabelValor"); // NOI18N
        getContentPane().add(jLabelValor, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 400, 70, 20));

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane3.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 430, 340, 60));

        jLabelObs.setText("Observações");
        jLabelObs.setName("jLabelObs"); // NOI18N
        getContentPane().add(jLabelObs, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 410, 80, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClienteActionPerformed
       daoCliente cli = new daoCliente();

        String cpf = jTextCPF.getText().toString();

        Vector<Cliente> vet = new Vector(cli.buscar(cpf));
        if(vet.isEmpty())
            JOptionPane.showMessageDialog(null, "Cliente nao Encontrado", "", 0);
        DefaultTableModel model = new DefaultTableModel(new Object[][]{}
                                , new Object[]{"Nome", "CPF","Telefone","Endereco"});

        for (Cliente cliente : vet) {
            model.addRow(new String[]{cliente.getNome(), cliente.getCPF(),cliente.getTelefone(),cliente.getEndereco()});
        }
        jTableCliente.setModel(model);
    }//GEN-LAST:event_jButtonClienteActionPerformed

    private void jButtonVeiculoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVeiculoActionPerformed
        daoVeiculo veiculo = new daoVeiculo();

        String modelo = jTextModelo.getText().toString();
        Vector<Veiculo> vet = new Vector(veiculo.buscar(modelo));
  
        if(vet.isEmpty())
            JOptionPane.showMessageDialog(null, "Veiculo nao encontrado", "", 0);
        DefaultTableModel model = new DefaultTableModel(new Object[][]{}
                                , new Object[]{"Chassi", "Modelo","Ano Modelo","Preco Venda"});

        for (Veiculo vei : vet) {
            model.addRow(new String[]{vei.getChassi(),vei.getModelo()," ",vei.getPreco()});
        }
        jTableVeiculo.setModel(model);
        jTextValor.setText(jTableVeiculo.getValueAt(0, 3).toString());
    }//GEN-LAST:event_jButtonVeiculoActionPerformed

    private void jButtonEfetuarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEfetuarActionPerformed
       //String nomeCliente = (String)jTableCliente.getValueAt(1, 1);
       String chassiVeiculo = jTableVeiculo.getValueAt(0, 0).toString();
       // System.out.println(jTableVeiculo.getValueAt(0, 0).toString());
       daoVeiculo veiculo = new daoVeiculo();
       daoCliente cliente = new daoCliente();
       Venda venda = new Venda();
      
       venda.setCliente(cliente.buscarCliente((String)jTableCliente.getValueAt(0, 1).toString()));

       Veiculo teste = veiculo.buscarChassi(chassiVeiculo);
     
       venda.setVeiculo(teste);
       venda.setValor(jLabelValor.getText());
       
    
      venda.efetivarNegociacao();
       JOptionPane.showMessageDialog(null, "Venda Efetuada", "", 3);
    }//GEN-LAST:event_jButtonEfetuarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new telaVendas().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCliente;
    private javax.swing.JButton jButtonEfetuar;
    private javax.swing.JButton jButtonVeiculo;
    private javax.swing.JLabel jLabelCPF;
    private javax.swing.JLabel jLabelCliente;
    private javax.swing.JLabel jLabelModelo;
    private javax.swing.JLabel jLabelObs;
    private javax.swing.JLabel jLabelValor;
    private javax.swing.JLabel jLabelVeiculo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTableCliente;
    private javax.swing.JTable jTableVeiculo;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextCPF;
    private javax.swing.JTextField jTextModelo;
    private javax.swing.JTextField jTextValor;
    // End of variables declaration//GEN-END:variables

}
